<template>
  <div id="home">
    <!-- <h2>首页</h2> -->
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>

    <tab-control
      :titles="['流行', '新款', '精选']"
      class="tabControl"
      @tabClick="tabClick"
      ref="tabControl"
      v-show="isTabFixed"
    ></tab-control>

    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scroll="contentscroll"
      :pull-up-load="true"
      @pullingUp="getMoreData"
    >
      <!-- 轮播图 -->
      <home-swiper
        :banners="banners"
        @swiperImageLoad="swiperImageLoad"
      ></home-swiper>
      <home-recommend-view :recommends="recommends"></home-recommend-view>
      <feature-view></feature-view>
      <tab-control
        :titles="['流行', '新款', '精选']"
        
        @tabClick="tabClick"
        ref="tabControl"
      ></tab-control>

      <!-- 展示商品 -->
      <goods-list :goods="showGoods"></goods-list>
    </scroll>
    <!-- 组件响应点击需要添加native属性 -->
    <back-top @click.native="backTop" v-show="backTopShow"></back-top>
  </div>
</template>

<script>
import NavBar from "components/common/navbar/NavBar.vue";
import HomeSwiper from "./childComps/HomeSwiper.vue";
import HomeRecommendView from "./childComps/HomeRecommendView.vue";
import FeatureView from "./childComps/featureView.vue";
import TabControl from "components/content/tabControl/TabControl.vue";
import GoodsList from "../../components/content/goods/GoodsList.vue";
import scroll from "components/common/scroll/Scroll.vue";
import backTop from "components/content/backTop/backTop.vue";

import { getHomeDate, getHomeGoods } from "network/home.js";

export default {
  name: "home",
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: {
        i: "pop",
        index: 0,
      },
      backTopShow: false,
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0,
    };
  },
  methods: {
    // 获取首页轮播图数据
    getHomeDate() {
      getHomeDate().then((res) => {
        // console.log(res.data);
        this.banners = res.data.banner.list;

        this.recommends = res.data.recommend.list;
      });
    },

    // 接收返回首页的请求
    backTop() {
      // console.log("组件被点击")
      this.$refs.scroll.scrollTo(0, 0, 500);
    },
    //获取首页商品数据
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        // console.log(res.data);
        this.goods[type].list.push(...res.data.list); //...可以解析数组，同时也可在声明函数时作为可变参数进行函数定义
        this.goods[type].page += 1;
        // console.log(res.data)
        this.$refs.scroll.scroll.finishPullUp();
      });
    },
    //获取点击的商品类别
    tabClick(i) {
      //  console.log(i.index)

      // console.log(this.currentType.i)

      switch (i.index) {
        case 0:
          this.currentType.i = "pop";
          break;
        case 1:
          this.currentType.i = "new";
          break;
        case 2:
          this.currentType.i = "sell";
          break;
      }
    },

    //显示和隐藏返回按钮
    contentscroll(position) {
      if (position.y <= -150) {
        this.backTopShow = true;
      } else {
        this.backTopShow = false;
      }
        // console.log(this.$refs.scroll.scroll)
        if(this.$refs.scroll.scroll.y<=-this.tabOffsetTop)
        {
         this.isTabFixed=true 
        }
        else{
          this.isTabFixed=false
        }
        
      
    },

    //上拉加载更多
    getMoreData() {
      this.getHomeGoods(this.currentType.i);
    },

    //轮播图图片已加载完成
    swiperImageLoad() {
      // console.log(this.$refs.tabControl.$el.offsetTop)
    },
  },
  components: {
    NavBar,
    HomeSwiper,
    HomeRecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    scroll,
    backTop,
  },

  created() {
    this.getHomeDate();
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },

  computed: {
    showGoods() {
      return this.goods[this.currentType.i].list;
    },
  },

  mounted() {
    //监听图片加载

    // const refresh=this.debounce(this.$refs.scroll.scroll.refresh,500)
    this.$bus.$on("itemImageLoad", () => {
      //通过防抖减少refresh的调用次数
      //  refresh();
      this.$refs.scroll.scroll.refresh();
      // console.log(this.$refs.scroll.scroll.refresh);
    });
    // this.$root.$on('itemImageLoad',() => {
    //   this.$refs.scroll.scroll.refresh();
    //   console.log("图片已加载");
    // })

    // console.log(this.$refs.tabControl.$el.offsetTop)
  },

  //数据更新监听
  updated() {
    // console.log(this.$refs.tabControl.$el.offsetTop)
    //$el用于获取组件中的元素
    this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop;
  },

  //
  activated(){
     this.$refs.scroll.scroll.scrollTo(0,this.saveY,1000)
     this.$refs.scroll.scroll.refresh();
  },
  deactivated(){
    
      this.saveY=this.$refs.scroll.scroll.y
  }
};
</script>

<style scoped>
.home-nav {
  background-color: rgb(226, 220, 220);
  /* position: fixed; */
  top: 0;
  z-index: 9;
}

#home {
  /* margin-top: 44px; */
  height: 100vh;
  position: relative;
}

.tabControl
{
  position: relative;
  z-index: 20;
}

.content {
  height: calc(100% - 93px);
  position: absolute;
  overflow: hidden;
  top: 44px;
  bottom: 49px;
}
</style>